# coding=utf8
__author__ = 'nickleo'
import ConfigParser


def singleton(cls, *args, **kw):
    instances = {}

    def _singleton():
        if cls not in instances:
            instances[cls] = cls(*args, **kw)
        return instances[cls]

    return _singleton


@singleton
class HogWartsConfig:
    def __init__(self):
        self._config = ConfigParser.ConfigParser()
        self._config.readfp(open('./config.ini'))
        print 'init   HogWartsConfig from file ./config.ini'

    def get_config(self, _scope, _key):
        if self._config is None:
            self.__init__()
        return self._config.get(_scope, _key)

    def get_global_config(self, _key):
        return self.get_config('global', _key)

    def get_nameservers(self):
        return self.get_global_config('nameservers').split(',')
